//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "CVerifBase.h"

//---------------------------------------------------------------------------
//              CONSTRUCTEUR
//---------------------------------------------------------------------------
__fastcall CVerifBase::CVerifBase()
{
    TZZMySqlDatabase * Base = NULL;
    try {

        Base = new TZZMySqlDatabase( (TComponent*)0);
        Base->Database = BASE_WEBHELP;
        Base->Login = LOGIN;
        Base->Host = ADRESSE_IP_BASE;
        Base->Port = PORT_BASE;
        try {
            Base->Connected = true;
        }catch (Exception &e) {
            Base->Connected = false;
            ExecuterScript(SCRIPT_WEBHELP);
        }

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(Base) {delete Base; Base = NULL; }
}
//---------------------------------------------------------------------------
//              VERIFIER LES TABLES
//---------------------------------------------------------------------------
bool __fastcall CVerifBase::VerifierTables()
{
    bool bRetour = false;
    try {
        if(     IsTablesExist("historique")
            &&  IsTablesExist("lien")
            &&  IsTablesExist("logiciels")
            &&  IsTablesExist("refer")
            &&  IsTablesExist("versions"))
        {
            bRetour = true;
        }else {
            //Creation des tables
            ExecuterScript(SCRIPT_WEBHELP);
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        bRetour = false;
    }
    return bRetour;
}
//---------------------------------------------------------------------------
//              EXISTANCES DES TABLES
//---------------------------------------------------------------------------
bool __fastcall CVerifBase::IsTablesExist(const AnsiString &NomTable )
{
    bool bRetour = false;
    TZZMySqlDatabase * BaseSystem = NULL;
    TZZMySqlTransact * Transaction = NULL;
    TZZMySqlQuery * MaRequete = NULL;
    try {

        BaseSystem = new TZZMySqlDatabase( (TComponent*)0);
        BaseSystem->Database = INFORMATION_SCHEMA;
        BaseSystem->Login = LOGIN;
        BaseSystem->Host  = ADRESSE_IP_BASE;
        BaseSystem->Port  = PORT_BASE;
        BaseSystem->Connected = true;

        Transaction = new TZZMySqlTransact((TComponent*)0);
        Transaction->Database   = BaseSystem;
        Transaction->AutoCommit = true;
        Transaction->Connected  = true;

        MaRequete = new TZZMySqlQuery((TComponent*)0);
        MaRequete->Database = BaseSystem;
        MaRequete->Transaction = Transaction;

        MaRequete->Sql->Text =  (AnsiString)    "SELECT count( table_name ) "+
                                                "FROM TABLES "+
                                                "WHERE table_name = \'"+NomTable+"\'";
        MaRequete->Open();
        MaRequete->First();
        if(nRequete(0) != 0) {
            bRetour = true;
        }

        MaRequete->Sql->Clear();
        MaRequete->Close();
        MaRequete->Transaction->Connected = false;

        BaseSystem->Connected = false;

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        bRetour = false;}
    if(MaRequete)   {delete MaRequete; MaRequete = NULL;}
    if(Transaction) {delete Transaction; Transaction = NULL;}
    if(BaseSystem)  {delete BaseSystem; BaseSystem = NULL; }
    return bRetour;
}

//---------------------------------------------------------------------------
//              EXECUTION DU METADATA
//---------------------------------------------------------------------------
void __fastcall CVerifBase::ExecuterScript(const AnsiString &NomMetadata )
{
    TZZMySqlDatabase * Base = NULL;
    TZZMySqlTransact * Transaction = NULL;
    TZZBatchSql * MySql_Script = NULL;
    try {

        Base = new TZZMySqlDatabase( (TComponent*)0);
        Base->Database = BASE_WEBHELP;
        Base->Login = LOGIN;
        Base->Host = ADRESSE_IP_BASE;
        Base->Port = PORT_BASE;
        Base->Connected = true;

        //creation de la base grace au webhelp.sql
        Transaction = new TZZMySqlTransact((TComponent*)0);
        Transaction->Database = Base;
        Transaction->AutoCommit = true;
        Transaction->Connected = true;
        MySql_Script = new TZZBatchSql( (TComponent*)0 );
        MySql_Script->Transaction = Transaction;
        MySql_Script->Sql->LoadFromFile(NomMetadata);

        if(FileExists(NomMetadata)) {
            MySql_Script->ExecSql();
            MySql_Script->Sql->Clear();
        }

        MySql_Script->Transaction->Connected = false;
        Base->Connected = false;
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MySql_Script) {delete MySql_Script; MySql_Script = NULL;}
    if(Transaction) {delete Transaction; Transaction = NULL;}
    if(Base) {delete Base; Base = NULL; }
}
//---------------------------------------------------------------------------
